package shake.el;

import java.beans.FeatureDescriptor;
import java.util.Iterator;

import javax.el.ELContext;
import javax.el.ELResolver;

import shake.annotation.ApplicationScoepd;
import shake.annotation.Component;

import com.google.inject.Inject;
import com.google.inject.Singleton;

@Component
@Singleton
public class VariableElResolver extends ELResolver {

	@Inject
	ELVariable variable;

	@Override
	public Class<?> getCommonPropertyType(ELContext arg0, Object arg1) {
		return null;
	}

	@Override
	public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext arg0,
			Object arg1) {

		return null;
	}

	@Override
	public Class<?> getType(ELContext arg0, Object arg1, Object arg2) {
		if (arg1 == null) {
			arg0.setPropertyResolved(true);
			return String.class;
		}
		return null;
	}

	@Override
	public Object getValue(ELContext arg0, Object arg1, Object arg2) {
		if (arg1 != null) {
			return null;
		}
		Object value = variable.get((String) arg2);
		if (value != null) {
			arg0.setPropertyResolved(true);
			return value;
		}

		return null;
	}

	@Override
	public boolean isReadOnly(ELContext arg0, Object arg1, Object arg2) {
		if (arg1 == null) {
			arg0.setPropertyResolved(true);
			return false;
		}
		return true;
	}

	@Override
	public void setValue(ELContext arg0, Object arg1, Object arg2, Object arg3) {
		if (arg1 == null) {
			arg0.setPropertyResolved(true);
			variable.put((String) arg2, arg3);
		}
	}

}
